package com.dyh.quickdemo.demos.UnsafeFunc;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/*
@Author=Joey Wheeler
@Date=2020/10/26 17:34
*/
public class UnsafeRes {
    volatile Object result = null;

    public void run(){
        for (;;){
            long resultAddr;
            try {
                resultAddr = UnsafeHelper.getUnsafe().objectFieldOffset(UnsafeRes.class.getDeclaredField("result"));
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
                break;
            }
            if (result!=null)
                break;
            UnsafeHelper.getUnsafe().compareAndSwapObject(this,resultAddr,null,1);
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        UnsafeRes unsafeRes = new UnsafeRes();
//        unsafeRes.run();
//        System.out.println(unsafeRes.result);
//        final CompletableFuture<Void> tryFire(int mode) {
//
//        }

        /*就因为是个Object*/
        CompletableFuture<String> cf = CompletableFuture.supplyAsync(()->"f")
                .thenApply(s->s+"!")
                .thenApply(s->s+"!");

        String res = cf.get();

        /*
        UniApply<T,V> c = new UniApply<T,V>(e, d, this, f);
            push(c); 信息都在c push c
        */

        System.out.println(res);
    }


}
